package com.foreknow.springboot_demo.controller;

import java.util.HashMap;
import java.util.Map;

import com.foreknow.springboot_demo.model.Admin;
import com.foreknow.springboot_demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private AdminService adminService;

    @RequestMapping("/login")
    public String login(Model model,Admin admin) {
        Admin adminRes = adminService.findByNameAndPassword(admin);
        if (adminRes!=null){
            return "redirect:dashboard";
        }else{
            model.addAttribute("error","用户名或密码错误......");
            return "login";
        }

    }

    @GetMapping("/register")
    public String register(Model model) {
        model.addAttribute("projectName", "MOXI");
        return "register";
    }

    @GetMapping("/dashboard")
    public String dashboard(Model model,Admin admin){
        return "dashboard";
    }


//    @RequestMapping("index")
//    public ModelAndView page1() {
//        return new ModelAndView("/index.html") ;
//    }
//
//    @RequestMapping("map")
//    public Map<String, String>  map1() {
//        Map<String, String> map = new HashMap<String, String>();
//        map.put("name", "张三");
//        map.put("age", "28");
//        return map;
//    }
//
//
//    @GetMapping("/admin/login")
//    public Admin login(Admin admin, ModelMap modelMap){
//        Admin admin1 = adminService.findByNameAndPassword(admin);
//        modelMap.addAttribute("userName",admin1.getUserName());
//        modelMap.addAttribute("password",admin1.getPassword());
//        return admin1;
//    }
}
